package com.fyzk.activity.myview;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.support.annotation.Px;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.FrameLayout;
import android.widget.Scroller;

import com.fyzk.utils.AppUtil;
import com.fyzk.utils.LogUtil;
import com.fyzk.utils.ScreenUtils;

public class DrawBallFrameLayout extends FrameLayout{
	
	private int x = 0, y = 0 , moveY = 0, lastY = 0;

	private BallScrollView scr;
	private BallPopFrameLayout fl_moveqiu;
	
	/**
	 * 滑动的最低端
	 */
	public int downY = 0;
	
	public static final int STATE_TOP = 0;
	public static final int STATE_DOWN = 1;
	public static final int STATE_CENTER_UPTODOWN = 2;
	public static final int STATE_CENTER_DOWNTOUP = 3;

	/**
	 * 移动状态
	 */
	public int state = 0;
	
	/**
	 * 是否是由上向下滑
	 */
	private boolean upToDown = true;

	private UpOrDownListener upOrDownListener;

	private Scroller mScroller;

	private boolean isAll = true;

	private int duration = 800;
	/**
	 * 构造函数
	 * @param context
	 * @param attrs
	 */
	public DrawBallFrameLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		if (isInEditMode()) {
			return;
		}
		setWillNotDraw(false);

		downY = AppUtil.getHeight()*250/560;
		scr = new BallScrollView(context);
		fl_moveqiu = new BallPopFrameLayout(context, attrs);

		mScroller = new Scroller(context);
	}

	//调用此方法滚动到目标位置
	public void smoothScrollTo(int fx, int fy) {
		int dx = fx - mScroller.getFinalX();
		int dy = fy - mScroller.getFinalY();
		smoothScrollBy(dx, dy);
	}

	//调用此方法设置滚动的相对偏移
	public void smoothScrollBy(int dx, int dy) {
		//设置mScroller的滚动偏移量
		mScroller.startScroll(mScroller.getFinalX(), mScroller.getFinalY(), dx, dy, duration);
		invalidate();//这里必须调用invalidate()才能保证computeScroll()会被调用，否则不一定会刷新界面，看不到滚动效果
	}

	@Override
	public void computeScroll() {
		//先判断mScroller滚动是否完成
		if (mScroller.computeScrollOffset()) {
			//这里调用View的scrollTo()完成实际的滚动
			scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
			//必须调用该方法，否则不一定能看到滚动效果
			postInvalidate();
		}
		super.computeScroll();
	}

	/**
	 * 拦截触屏事件
	 */
	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN:
			y = (int) ev.getY();
			x = (int) ev.getX();
			return false;
		case MotionEvent.ACTION_MOVE:

			if (!scr.isHDTop()) {

				if (Math.abs(y - (int)ev.getY()) > 3 || Math.abs(x - (int)ev.getX()) > 20) {
					Rect rect = new Rect(0, 0, 0, 0);
					fl_moveqiu.setRect(rect, "",0,false);
				}
				return false;
			}
			if (Math.abs(y - (int)ev.getY()) > 3 || Math.abs(x - (int)ev.getX()) > 20) {
				Rect rect2 = new Rect(0, 0, 0, 0);
				fl_moveqiu.setRect(rect2, "",0,false);
				if (state == STATE_TOP && y < (int)ev.getY()) {
					return true;
				}else if(state == STATE_DOWN && y > (int)ev.getY()){
//					hua_fl = true;
//					hua_scrl = false;
					return true;

				}else if (state == STATE_CENTER_UPTODOWN || state == STATE_CENTER_DOWNTOUP){
					return true;
				} else {
					return false;
				}
			}else {
				return false;
			}



		case MotionEvent.ACTION_UP:
			return false;
		default:
			break;
		}

		return super.onInterceptTouchEvent(ev);
	}

	private boolean isClickDownLine = false;
	protected boolean isClickTrend = true;
	/**
	 * 触屏事件
	 */
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		isClickDownLine = false;
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
//			Log.e("pl move11111", "y: "+y+"  gety: "+(int)event.getY());
			float x2 = event.getX();
			float y2 = event.getY()+this.getScrollY();
			Rect rect = new Rect(0,0, ScreenUtils.getInstance().getWidth(), ScreenUtils.getInstance().getWidth()/13);

			if (rect.contains((int)x2,(int)y2)) {
				if (state == STATE_TOP) {
					upToDown = true;
					isAll = false;
				} else if (state == STATE_DOWN) {
					upToDown = false;
					isAll = false;
				} else if (state == STATE_CENTER_UPTODOWN) {
					upToDown = true;
					isAll = true;
				} else if (state == STATE_CENTER_DOWNTOUP) {
					upToDown = false;
					isAll = true;
				}
				isClickDownLine = true;
				break;
			}
			isClickTrend = true;
			break;
		case MotionEvent.ACTION_MOVE:
			if (isClickDownLine) {
				break;
			}
			if (Math.abs(y - (int)event.getY()) > 10) {
				isClickTrend = false;
				upToDown = moveY - (int)event.getY() < 0;
				moveY = (int)event.getY();

				int scrollY = lastY - (moveY - y);
				isAll = Math.abs(lastY-scrollY) >= Math.abs(downY)/2;
				if (state == STATE_CENTER_UPTODOWN) {
					isAll = (Math.abs(scrollY) < Math.abs(downY)/2 && !upToDown) ||
							(Math.abs(scrollY) > Math.abs(downY)/2 && upToDown);
				}
				if (state == STATE_CENTER_DOWNTOUP) {
					isAll = (Math.abs(scrollY) < Math.abs(downY)/2 && !upToDown) ||
							(Math.abs(scrollY) > Math.abs(downY)/2 && upToDown);
				}
				if (scrollY >=-downY && scrollY <= 0) {
					this.smoothScrollTo(0, scrollY);
				}
			}
			
			break;
		case MotionEvent.ACTION_UP:
			LogUtil.d("upToDown:"+upToDown+", isAll:"+isAll);
			float xTrend = event.getX();
			Rect rectTrend = new Rect(0,0, ScreenUtils.getInstance().getWidth(), -getScrollY());
			if (rectTrend.contains((int)xTrend,(int)event.getY()) && state != STATE_TOP && isClickTrend) {
				if (upOrDownListener != null) {
					upOrDownListener.toTrend();
				}
			}
			switch (state) {
				case STATE_TOP:
					if (upToDown) {
						if (isAll) {
							state = STATE_DOWN;
							lastY = -downY;
						} else {
							state = STATE_CENTER_UPTODOWN;
							lastY = -downY/2;
						}
					} else {
						state = STATE_TOP;
						lastY = 0;
					}
					break;
				case STATE_CENTER_UPTODOWN:
					if (upToDown && isAll) {
						state = STATE_DOWN;
						lastY = -downY;
					} else if (!upToDown && isAll){
						state = STATE_TOP;
						lastY = 0;
					} else {
						state = STATE_CENTER_UPTODOWN;
						lastY = -downY/2;
					}
					break;
				case STATE_CENTER_DOWNTOUP:
					if (!upToDown && isAll) {
						state = STATE_TOP;
						lastY = 0;
					} else if (upToDown && isAll) {
						state = STATE_DOWN;
						lastY = -downY;
					} else {
						state = STATE_CENTER_DOWNTOUP;
						lastY = -downY/2;
					}
					break;
				case STATE_DOWN:
					if (upToDown) {
						state = STATE_DOWN;
						lastY = -downY;
					} else {
						if (isAll) {
							state = STATE_TOP;
							lastY = 0;
						} else {
							state = STATE_CENTER_DOWNTOUP;
							lastY = -downY/2;
						}
					}
					break;
			}
			this.smoothScrollTo(0, lastY);
			break;
		default:
			break;
		}
		
		return true;
	}

	@Override
	public void scrollTo(@Px int x, @Px int y) {
		super.scrollTo(x, y);
		if (upOrDownListener != null) {
			upOrDownListener.upOrDownState(state);
		}
	}

	@Override
	public void setOnTouchListener(OnTouchListener l) {
		// TODO Auto-generated method stub
		super.setOnTouchListener(l);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
	}

	public int getY1() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public int getX1() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public void setScr(BallScrollView scr) {
		this.scr = scr;
	}

	public void setFl_moveqiu(BallPopFrameLayout fl_moveqiu) {
		this.fl_moveqiu = fl_moveqiu;
	}

	public void setUpOrDownListener(UpOrDownListener upOrDownListener) {
		this.upOrDownListener = upOrDownListener;
	}

	public interface UpOrDownListener {
		void upOrDownState(int state);
		void toTrend();
	}
}
